import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

// TODO: Restructure GIT repository and commit your stuff
// TODO: Merge player with main frame, keep player invisible but attach URL control to control play/stop
// TODO: POSSIBLY make the browser load an HTML page with an embedded JS script if we can use that to better control the player
// TODO: Add functions to play a specific video, play from a specific time, autoplay on/off, etc.

public class YouTubePlayer {	
	public static void main(String[] args) {
		
	    NativeInterface.open();
	    SwingUtilities.invokeLater(new Runnable() {
	        public void run() {
	            JFrame frame = new JFrame("YouTube Viewer");
	            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	            frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);
	            frame.setSize(800, 600);
	            frame.setLocationByPlatform(true);
	            frame.setVisible(true);
	        }
	    });
	    NativeInterface.runEventPump();
	    // don't forget to properly close native components
	    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
	        @Override
	        public void run() {
	            NativeInterface.close();
	        }
	    }));
	    
		
		// Testing YouTube info/data retrieval

		
		/*
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			
			//FEED TEST
			System.out.print("Enter search query: ");
			String searchTest = br.readLine();
			
			YouTubeFeedRetriever feedRetriever = new YouTubeFeedRetriever();			
			for (YouTubeFeedEntry entry : feedRetriever.getVideos(searchTest)) {
				System.out.println(entry.toString() + "\n");
			}
		} catch (Exception e) {
			System.out.println("[YouTubePlayer - main] Failed: " + e.toString());
		}
		*/
	}

	public static JPanel getBrowserPanel() {
	    JPanel webBrowserPanel = new JPanel(new BorderLayout());
	    JWebBrowser webBrowser = new JWebBrowser();
	    JButton playButton = new JButton();
	    
	    webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
	    webBrowser.setVisible(true);
	    webBrowser.setBarsVisible(false);
	    webBrowser.navigate("https://www.youtube.com/v/zfJ7vwhlFLE?fs=1");
	    /*
	    webBrowserPanel.add(playButton, BorderLayout.SOUTH);
	    playButton.setVisible(true);
	    playButton.setText("Play");
	    playButton.addActionListener(new Action() {
	    	public void actionPerformed(ActionEvent e) {
	    		System.out.println("Button clicked");
	    	}

			@Override
			public void addPropertyChangeListener(PropertyChangeListener arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public Object getValue(String arg0) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public boolean isEnabled() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void putValue(String arg0, Object arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void removePropertyChangeListener(PropertyChangeListener arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void setEnabled(boolean arg0) {
				// TODO Auto-generated method stub
				
			}
	    });
	    */
	    
	    return webBrowserPanel;
	}
}